import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

import static java.lang.Math.max;

public class BinaryTree {

    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }

        public TreeNode root;

        // 创建一颗树,返回根节点
    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    // 前序遍历
    public void prevOrder(TreeNode root){
        if(root == null){
            return;
        }

        System.out.print(root.val + " ");
        prevOrder(root.left);
        prevOrder(root.right);
    }

    // 利用栈进行非递归前序遍历
    public void prOrder(TreeNode root){
         if(root == null){
             return;
         }

         Stack<TreeNode> stack = new Stack<>();
         TreeNode cur = root;
         while(cur != null || !stack.isEmpty()){
             while(cur != null){
                 stack.push(cur);
                 System.out.print(cur.val + " ");
                 cur = cur.left;
             }

             TreeNode top = stack.pop();
             cur = top.right;
         }
    }


    // 中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 利用非递归栈写中序遍历
    public void inOrderNor(TreeNode root){
        if(root == null){
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            // cur为空,弹出元素打印
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    // 后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 利用栈实现非递归写后序遍历
    public void postOrderNor(TreeNode root){
        if(root == null){
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;

        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }

            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev){
                System.out.print(top.val + " ");
                stack.pop();
                prev = top;
            }else{
                cur = top.right;
            }
        }
    }

    public int count = 0;

    // 获取树节点的个数
    public int size(TreeNode root){
        if(root == null){
            return 0;
        }

        int left = size(root.left);
        int right = size(root.right);

        return left + right + 1;
    }

    /*
    public int size(TreeNode root){
        if(root == null){
            return 0;
        }

        count++;
        size(root.left);
        size(root.right);

        return count;
    }
    */

    // 获取叶子节点的个数
    public int rootsize(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }

        int left = rootsize(root.left);
        int right = rootsize(root.right);

        return left + right;
    }

    // 获取第k层节点的个数
    // 整棵树的第k层节点个数等于左树的第k-1层+右树的k-1层节点个数
    // 比如第3层节点个数 = 左树第2层节点 + 右树第2层节点
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }

        int leftSize = getKLevelNodeCount(root.left,k-1);
        int rightSize = getKLevelNodeCount(root.right,k - 1);

        return leftSize + rightSize;
    }

    // 获取二叉树的高度
    /*public int getHigh(TreeNode root){
        if(root == null){
            return 0;
        }

        int leftHigh = getHigh(root.left);
        int rightHigh = getHigh(root.right);

        return max(leftHigh,rightHigh) + 1;
    }*/

    // 获取二叉树的高度
    public int getHigh(TreeNode root){
        if(root == null){
            return 0;
        }

        // return max(getHigh(root.left),getHigh(root.right)) + 1;
        return getHigh(root.left) > getHigh(root.right) ?
               getHigh(root.left) + 1 : getHigh(root.right) + 1;
    }

    // 查找值为val的节点
    public TreeNode find(TreeNode root,char val){
        if(root == null){
            return null;
        }

        if(root.val == val){
            return root;
        }
        TreeNode leftVal = find(root.left,val);
        // 不为空说明找到了,如果这里写leftVal.val == val
        // 会造成空指针异常,leftVal可能为空指针
        if(leftVal != null){
            return leftVal;
        }
        TreeNode rightVal = find(root.right,val);
        if(rightVal != null){
            return rightVal;
        }

        return null;
    }

    // 层序遍历
    public void levelOrder(TreeNode root){
        if(root == null){
            return ;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");

            if (cur.left != null) {
                queue.offer(cur.left);
            }

            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    // 判断一棵树是不是一颗完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if(root == null){
            return true;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();

            if(tmp == null){
                break;
            }else{
                queue.offer(tmp.left);
                queue.offer(tmp.right);
            }
        }


        while(!queue.isEmpty()){
            // 一个一个元素出队,判断是否为空
            // 不为空就不是完全二叉树
            TreeNode tmp1 = queue.peek();
            if(tmp1 != null){
                return false;
            }else{
                queue.poll();
            }
        }

        return true;
    }
}

/*
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }

        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root,p,stack1);
        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root,q,stack2);

        int size = 0;
        if(stack1.size() > stack2.size()){
            size = stack1.size() - stack2.size();
            while(size != 0){
                stack1.pop();
                size--;
            }
        }else{
            size = stack2.size() - stack1.size();
            while(size != 0){
                stack2.pop();
                size--;
            }
        }

        while(!stack1.empty()){
            TreeNode root1 = stack1.pop();
            TreeNode root2 = stack2.pop();

            if(root1 == root2){
                return root1;
            }
        }

        return null;
    }

    private boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root == null || node == null){
            return false;
        }

        stack.push(root);
        if(root == node){
            return true;
        }

        boolean left = getPath(root.left,node,stack);
        if(left == true){
            return true;
        }

        boolean right = getPath(root.right,node,stack);
        if(right == true){
            return true;
        }

        stack.pop();

        return false;
    }
}
*/
